import Foundation
import Alamofire

struct ActionDetail:Decodable{
    var actionId:Int
    var actionName:String
    var actionUrl: String
    var duration: String
    var introId: Int
    var intro: String
}

class ActionDetailModel : ObservableObject{
    //数据
    @Published var actionList :[ActionDetail] = []{
        didSet{
            if(actionList.count > 0){
                curDetail = actionList[0]
            }
        }
    }
    @Published var curIdx = 0{
        didSet{
            curDetail = actionList[curIdx]
        }
    }
    @Published var relatedId : Int = 1
    @Published var curDetail:ActionDetail?=nil
    
    var total :Int{
        return actionList.count
    }
    
    func fetchActionDataBycid(){
        
        var headers: HTTPHeaders = [
           "Content-Type": "application/json"
       ]
        
        var parameters = [
            "id": self.relatedId,
        ]
        
        let request = AF.request("https://www.fastmock.site/mock/318b7fee143da8b159d3e46048f8a8b3/api/courseId2ActionList"
                       ,method: .get
                       ,parameters: parameters
                       ,encoding: URLEncoding.default
                       ,headers: headers
                        )
            //该如何使用
        request.responseDecodable(of: ResponseData<[ActionDetail]>.self) { (response) in
            print(response)
            guard let res = response.value else { return }

            switch res.status {
                case .success:
                    self.actionList = res.data
                case .failed:
                    print("faild")
            }
        }
        
    }
    
    func getNextAction(){
        if self.curIdx >= (self.total - 1){
            return
        }
        
        self.curIdx += 1
    }
    
    func getPreviousAction() {
        if self.curIdx <= 0 {
            return
        }
        self.curIdx -= 1
    }
}


